package com.yj.event_listener.contentObserver;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.CallLog;

import androidx.annotation.Nullable;

import com.yj.event_listener.constants.Constants;
import com.yj.event_listener.interfaces.NotificationCallback;
import com.yj.event_listener.util.LogUtil;
import com.yj.event_listener.util.PhoneUtil;
import com.yj.event_listener.util.SmsUtil;

public class PhoneContentObserver extends ContentObserver {

    private final Context context;
    private final NotificationCallback notificationCallback;
    private final Handler handler;

    /**
     * Creates a content observer.
     *
     * @param handler The handler to run {@link #onChange} on, or null if none.
     */
    public PhoneContentObserver(Handler handler, Context context, NotificationCallback callback) {
        super(handler);
        this.context = context;
        this.notificationCallback = callback;
        this.handler = handler;
    }


    @Override
    public void onChange(boolean selfChange) {
        LogUtil.d("onChange");
        onChange(selfChange, null);
    }

    private Long lastChange = 0L;
    @Override
    public void onChange(boolean selfChange, @Nullable Uri uri) {
        long current = System.currentTimeMillis();
        LogUtil.d("onChange "+ uri + " ringing " + Constants.ringing
                + " hangup " + Constants.hangup
                + " lastChange " + lastChange + " current " + current);
        if(current - lastChange < 3000 || !Constants.hangup){
            return;
        }
        lastChange = current;
//        super.onChange(selfChange, uri);
        LogUtil.d("delay 2000 ms to execute queryFirstMissedCall");
        handler.postDelayed(this::queryFirstMissedCall, 2000);

    }

    private void queryFirstMissedCall(){
        Cursor cursor = PhoneUtil.getFirstMissedCall(context);
        // Process the cursor to get the first SMS message
        if (cursor != null && cursor.moveToFirst()) {
            int typeIndex = cursor.getColumnIndex(CallLog.Calls.TYPE);
            if(typeIndex != -1){
                int type = cursor.getInt(typeIndex);
                LogUtil.d("type "+type);
                if(type == CallLog.Calls.MISSED_TYPE){
                    int nameIndex = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
                    int numIndex = cursor.getColumnIndex(CallLog.Calls.NUMBER);
                    if(nameIndex != -1 && numIndex != -1){
                        String name = cursor.getString(nameIndex);
                        LogUtil.d("name "+name);
                        String number = cursor.getString(numIndex);
                        LogUtil.d("number "+number);
                        if(notificationCallback != null){
                            notificationCallback.onMissedCall("", name == null || name.isEmpty() ? number : name);
                        }
                    }


                }
            }

            // Close the cursor
            cursor.close();
        }
    }
}
